package javango.test;

import java.util.HashMap;
import java.util.Map;

import com.google.inject.Injector;

import javango.contrib.sessions.Session;
import javango.core.User;
import javango.core.servers.RequestProcessor;
import javango.http.HttpException;
import javango.http.HttpRequest;
import javango.http.HttpResponse;

public class Client {
	// TODO Probably should allow Guice to get this instance,  giving ability for a singleton or an object per requst
	RequestProcessor requestProcessor;
	HttpRequest request;
	User user;
	Session session = new TestSession();
	
	public Client(Injector injector) throws Exception {
		requestProcessor = injector.getInstance(RequestProcessor.class);
	}
	
	public HttpResponse get(String url) throws HttpException {
		if (request != null) request.close();
		request = new HttpRequest();
		request.setContext("/");
		request.setUser(user);
		request.setPath(url);
		request.setMethod("GET");
		request.setSession(session);
		request.setParameterMap(new HashMap<String, String[]>());
		return requestProcessor.service(request);
	}
	
	public HttpResponse post(String url) throws HttpException {
		return post(url, new HashMap<String, String[]>());
	}
	public HttpResponse post(String url, Map<String, String[]> postData) throws HttpException {
		if (request != null) request.close();	
		request = new HttpRequest();
		request.setContext("/");
		request.setUser(user);
		request.setPath(url);
		request.setMethod("POST");
		request.setSession(session);
		request.setParameterMap(postData);
		return requestProcessor.service(request);
	}
	
	public void close() throws HttpException {
		if (request != null) request.close();
		request = null;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

}
